<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/socket.io/2.1.1/socket.io.dev.js"></script>
    <style>
        .list{
            height: 200px;
            overflow-y: scroll;
        }
    </style>
</head>
<body>
<p>
    姓名：
    <input type="text" id="name">
</p>
<p>
    说：
    <input type="text" id="txt">
</p>
<input type="button" value="submit" id="submit">
<div class="list">

</div>
<script>
    let url = 'ws://127.0.0.1:5000/chat';
    let socket = io.connect(url);

    socket.on('connect', function () {
        alert('success')
    });

    //监听回复的消息
    socket.on('chat request', function (data) {
        let html = $('.list').html();
        let text = '<p>' + data.name + '说：' + data.msg + '</p>' + html;
        $('.list').html(text);
    });

    $(function () {
        $('#submit').on('click', function () {
            let val = $('#txt').val();
            $('#txt').val('');
            socket.emit('chat message', {'param': val, 'name': $('#name').val()});
        });
    });
</script>
</body>
</html>
